package com.amikomgamedev.labirinhantu.entity;

import java.util.ArrayList;

import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.AnimatedSprite;

import com.amikomgamedev.labirinhantu.grafik.Grafik_Anim;

public class Entity_Fire	implements Grafik_Anim
{
	private ArrayList<Rectangle>		lst_Rectangle	= new ArrayList<Rectangle>();
	private ArrayList<AnimatedSprite>	lst_Fire	= new ArrayList<AnimatedSprite>();

	private float[] m_Fire_Width	= {0, 0, 0, 0, 0, 0, 0, 0, 0};
	private float[] m_Fire_Height	= {0, 0, 0, 0, 0, 0, 0, 0, 0};
	
	private	float m_Time_Cur		= 0;
	private float m_Time_Cur_Max	= 4;
	private float m_Time_Cur_Min	= 2;
	
	public Entity_Fire(ArrayList<AnimatedSprite> p_Fire
		, ArrayList<Rectangle> p_Rec)
	{
		lst_Rectangle	= p_Rec;
		lst_Fire	= p_Fire;

//		Utils.TRACE("lst_Game_Play_Fire = " + lst_Rectangle.size());
		for (int i = 0; i < lst_Rectangle.size(); i++) {
			m_Fire_Width[i]		= lst_Rectangle.get(i).getWidth();
			m_Fire_Height[i]	= lst_Rectangle.get(i).getHeight();
			
//			if (m_Fire_Width[i] < m_Fire_Height[i])
//			    lst_Fire.get(i).setRotation(90f);
		}
	}
	
	public void onUpdate(float p_Sec)
	{
		m_Time_Cur+=p_Sec;
		
		if (m_Time_Cur % m_Time_Cur_Max < m_Time_Cur_Min)
			for (int i = 0; i < lst_Rectangle.size(); i++)
			{
			    	if (lst_Fire.get(i).isAnimationRunning())
					lst_Fire.get(i).stopAnimation(0);
			    	
				if (m_Fire_Width[i] < m_Fire_Height[i])
					lst_Rectangle.get(i).setHeight(0);
				else
					lst_Rectangle.get(i).setWidth(0);
			}
		else if (m_Time_Cur % m_Time_Cur_Max < (m_Time_Cur_Max + m_Time_Cur_Min) / 2)
			for (int i = 0; i < lst_Rectangle.size(); i++)
			{
			    	if (!lst_Fire.get(i).isAnimationRunning())
					lst_Fire.get(i).animate((333));
				if (m_Fire_Width[i] < m_Fire_Height[i])
					lst_Rectangle.get(i).setHeight(
							(m_Time_Cur_Min - m_Time_Cur % m_Time_Cur_Max)
							* - m_Fire_Height[i]);
				else
					lst_Rectangle.get(i).setWidth(
							(m_Time_Cur_Min - m_Time_Cur % m_Time_Cur_Max)
							* - m_Fire_Width[i]);
			}
		else if (m_Time_Cur % m_Time_Cur_Max < m_Time_Cur_Max)
			for (int i = 0; i < lst_Rectangle.size(); i++)
			{
				if (m_Fire_Width[i] < m_Fire_Height[i])
					lst_Rectangle.get(i).setHeight(
							(m_Time_Cur_Max - m_Time_Cur % m_Time_Cur_Max)
							* m_Fire_Height[i]);
				else
					lst_Rectangle.get(i).setWidth(
							(m_Time_Cur_Max - m_Time_Cur % m_Time_Cur_Max)
							* m_Fire_Width[i]);
			}

	}
}
